package com.wj.facedetect.activity.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.wj.facedetect.R;
import com.wj.facedetect.activity.base.BaseActivity;
import com.wj.facedetect.bean.home.NTONResultsBean;
import com.wj.facedetect.bean.home.RefactorBean;
import com.wj.facedetect.net.ReqUrls;
import com.wj.facedetect.util.CommonUtils;
import com.wj.facedetect.util.PreferencesUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class FaceCheckResultTempActivity extends BaseActivity {
    private static final String TAG = "FaceCheckResultTempActivity";
    private RecyclerView recyclerView;
    private TextView tvLeft, tvRight;
    private FaceCheckResultAdapter adapter;
    private RelativeLayout rlNodata;
    private NTONResultsBean ntonResultsBean;
    private List<RefactorBean> resultsAll = new ArrayList<>();

    public FaceCheckResultTempActivity() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_face_check_result);
        ntonResultsBean = (NTONResultsBean) getIntent().getSerializableExtra("results");
        int suitability = PreferencesUtils.getInstance().getSharePreSuitability(PreferencesUtils.SUITABILITY);
        if (ntonResultsBean != null) {
            List<RefactorBean> results = ntonResultsBean.getResultsAll();
            if (results != null) {
                for (RefactorBean bean : results) {
                    List<RefactorBean.ResultsBean> results1 = bean.getResults();
                    if (results1 != null && results1.size() > 0) {
                        if (results1.get(0).getSimilarity() >= suitability) {
                            resultsAll.add(bean);
                        }
                    }
                }
            }
        }
        initView();
    }

    @Override
    public void onBackPressed() {
        setResult(RESULT_OK);
        finish();
        super.onBackPressed();
    }

    private void initView() {
        tvLeft = (TextView) findViewById(R.id.tv_left);
        tvLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setResult(RESULT_OK);
                finish();
            }
        });
        tvRight = (TextView) findViewById(R.id.tv_right);
        tvRight.setVisibility(View.INVISIBLE);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        adapter = new FaceCheckResultAdapter();
        recyclerView.setAdapter(adapter);
        rlNodata = (RelativeLayout) findViewById(R.id.rl_no_data);
        if (resultsAll.size() == 0) {
            rlNodata.setVisibility(View.VISIBLE);
        } else {
            rlNodata.setVisibility(View.GONE);
        }
    }


    class FaceCheckResultAdapter extends RecyclerView.Adapter<FaceCheckResultAdapter.MyViewHolder> {

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            final RefactorBean refactorBean = resultsAll.get(position);
            List<RefactorBean.ResultsBean> results = refactorBean.getResults();
            if (results != null && results.size() > 0) {
                RefactorBean.ResultsBean resultBean = results.get(0);
                double similarity = resultBean.getSimilarity();
                BigDecimal b = new BigDecimal(similarity);
                double ppd = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                holder.tvSim.setText(ppd + "");
                holder.tvSize.setText("检测到" + results.size() + "个结果");
                holder.tvName.setText(resultBean.getName());
                String photoUrl1 = CommonUtils.StringToBase64(CommonUtils.trimPhotoAtdefault(refactorBean.getFace_image_uri()));
                String photoUrl2 = CommonUtils.StringToBase64(CommonUtils.trimPhotoAtdefault(resultBean.getFace_image_uri()));
                String photoUrl1Temp = photoUrl1.replace("\n", "");
                String photoUrl2Temp = photoUrl2.replace("\n", "");
                String uri1 = PreferencesUtils.getInstance().getSharePreStrBaseUrl(PreferencesUtils.BASE_URL)
                        + String.format(ReqUrls.URL_GET_PHOTO2, PreferencesUtils.getInstance().getSharePreStrClusterId(PreferencesUtils.CLUSTER_ID), photoUrl1Temp);
                String uri2 = PreferencesUtils.getInstance().getSharePreStrBaseUrl(PreferencesUtils.BASE_URL)
                        + String.format(ReqUrls.URL_GET_PHOTO2, PreferencesUtils.getInstance().getSharePreStrClusterId(PreferencesUtils.CLUSTER_ID), photoUrl2Temp);
                holder.headView1.setImageURI(uri1);
                holder.headView2.setImageURI(uri2);
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(FaceCheckResultTempActivity.this, FaceCheckResult2Activity.class);
                        intent.putExtra("refactorBean", refactorBean);
                        startActivity(intent);
                    }
                });
            }

        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View inflate = null;
            inflate = LayoutInflater.from(FaceCheckResultTempActivity.this).inflate(R.layout.facecheckresult_item, parent, false);
            return new MyViewHolder(inflate);
        }

        @Override
        public int getItemCount() {
            return resultsAll == null ? 0 : resultsAll.size();
        }

        class MyViewHolder extends RecyclerView.ViewHolder {
            private SimpleDraweeView headView1, headView2;
            private TextView tvName, tvSim, tvSize;

            public MyViewHolder(View itemView) {
                super(itemView);
                headView1 = itemView.findViewById(R.id.iv_head_1);
                headView2 = itemView.findViewById(R.id.iv_head_2);
                tvName = itemView.findViewById(R.id.name);
                tvSim = itemView.findViewById(R.id.sim);
                tvSize = itemView.findViewById(R.id.result_size);
            }
        }
    }
}
